package com.example.wificovv2;

import java.util.ArrayList;
import java.util.HashMap;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import com.google.android.gms.R.color;
import com.google.android.gms.internal.db;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import android.app.Activity;
import android.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.google.android.gms.maps.MapFragment;
import com.example.database.myDBHelper;
import com.example.wificov.SamplingPowPic;



public class Displaypow extends Activity {
	myDBHelper db;
	GoogleMap myMap;
    UiSettings uisetting;
    ArrayList<PolygonOptions> gonOpts;
    PolygonOptions gonOpt1, gonOpt2, gonOpt3;
    Polygon polygon;
    ArrayList<LatLng> level1, level2, level3;
    ArrayList<ArrayList<LatLng>> levels;
    float x, y;
    Button load, save, finish;
    ListView lvssid ;
    String value = "";
    int APID;
    
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		FragmentManager myFragmentManager = getFragmentManager();
        MapFragment myMapFragment = (MapFragment)myFragmentManager.findFragmentById( R.id.mappow );
    	this.myMap = myMapFragment.getMap();
        
	    load = (Button) findViewById(R.id.loadmappow);
	    save = (Button) findViewById(R.id.powmapsave);
	    finish = (Button) findViewById(R.id.finishmappow);
	    lvssid = (ListView) findViewById(R.id.ssidmap);
        // Lấy giá toạ độ của AP từ database cho 2 biến
	    
	    lvssid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			 public void onItemClick(
			 AdapterView<?> parent, View view, int position, long id) {
				 String key = "";				 
				 HashMap<String, String> item = (HashMap<String, String>) parent.getAdapter().getItem(position);
				 for (HashMap.Entry<String,String> entry : item.entrySet()) {
					// key = entry.getKey();
					 value = entry.getValue();					
					}
			 }
			 });
	    
	    finish.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Displaypow.this, Wlanmap.class);
				startActivity(intent);
			}
		});
	    
	    
	    load.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
			//Xét listview được chọn là ssid nào 
				
//				APID = db.getAPName(value);	
				String[] position = db.getAPpos(APID);
			//Lấy dữ liệu từ ssid đó đưa vào mảng data1 hoặc 2
				float x = Float.parseFloat(position[0]);
				float y = Float.parseFloat(position[1]);
				
				// Getting reference to SupportMapFragment of the activity_main
		        MapFragment fm = (MapFragment)getFragmentManager().findFragmentById( R.id.mappow );
		 
		        // Getting Map for the SupportMapFragment
		        myMap = fm.getMap();
		 
		        // Enable MyLocation Button in the Map
		        myMap.setMyLocationEnabled( true );
		        uisetting = myMap.getUiSettings();
		        uisetting.setZoomControlsEnabled( false );
		        	        
				// Thay hai giá trị này vào đây
		        myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(x, y), 14));		        
		        // Chuyển các chuỗi sau thành chuỗi dữ liệu lấy từ database theo từng level
		        /*get array points
		         * gonOpts = new arraylist<polygonoptions>;
		         * select max level where ssid..
		         * select * from poplygon where level <= maxlevel
		         * for(int k)
		         * {
		     * 			gonOpt3 = new PolygonOptions();
		         * 		lvel = new ArrayList<LatLng>();
		         * 			for(int i = 0; i < array.length; i++ )
		         * 			{
		         * 			lvel.add(new LatLng(array[i][x], array[i][y]));
		     * 				}
		     *			gonOpt3.addAll(lvel);
		    			gonOpt3.fillColor(polygonArray[k][color]);
		    			gonOpt3.strokeColor(polygonArray[k][color]);
		        		gonOpt3.zIndex(polygonArray[k][index]);
		        		gonOpts.Add(gonOpt3);
		     *		}
		         * */
		        level1 = new ArrayList<LatLng>();
		        level1.add(new LatLng(10.758057883887323, 106.66831970214844));
		        level1.add(new LatLng(10.75763627288421, 106.67055130004883));
		        level1.add(new LatLng(10.75759411175148, 106.67171001434326));
		        level1.add(new LatLng(10.757467628317917, 106.67299747467041));
		        level1.add(new LatLng(10.756160629731594, 106.6741132736206));
		        level1.add(new LatLng(10.755190917443025, 106.67312622070312));
		        level1.add(new LatLng(10.75498011001124, 106.67205333709717));
		        level1.add(new LatLng(10.755781177468222, 106.67055130004883));
		        level1.add(new LatLng(10.756708726602554, 106.66926383972168));
		        level1.add(new LatLng(10.75831085020626, 106.66823387145996));
		        
		        level2 = new ArrayList<LatLng>();
		        level2.add(new LatLng(10.759533517756049, 106.66600227355957));
		        level2.add(new LatLng(10.75949135688858, 106.66844844818115));
		        level2.add(new LatLng(10.759407035135983, 106.6714096069336));
		        level2.add(new LatLng(10.76008160849654, 106.67308330535889));
		        level2.add(new LatLng(10.760039447705722, 106.67462825775146));
		        level2.add(new LatLng(10.759533517756049, 106.67698860168457));
		        level2.add(new LatLng(10.758563816312968, 106.67861938476562));
		        level2.add(new LatLng(10.75738330599941, 106.67874813079834));
		        level2.add(new LatLng(10.755486047600009, 106.67844772338867));
		        level2.add(new LatLng(10.754642817813954, 106.6775894165039));
		        level2.add(new LatLng(10.75358877726667, 106.67625904083252));
		        level2.add(new LatLng(10.753167160016577, 106.6746711730957));
		        level2.add(new LatLng(10.752661218538718, 106.67205333709717));
		        level2.add(new LatLng(10.75358877726667, 106.6697359085083));
		        level2.add(new LatLng(10.755443886166693, 106.66746139526367));
		        level2.add(new LatLng(10.756540081517578, 106.6666030883789));
		        level2.add(new LatLng(10.758226528123522, 106.66634559631348));
		        
		        level3 = new ArrayList<LatLng>();
		        level3.add(new LatLng(10.761304268865539, 106.66844844818115));
		        level3.add(new LatLng(10.76252692427571, 106.6714096069336));
		        level3.add(new LatLng(10.762105319522043, 106.67763233184814));
		        level3.add(new LatLng(10.76147291128591, 106.68097972869873));
		        level3.add(new LatLng(10.757678434011046, 106.68424129486084));
		        level3.add(new LatLng(10.754432009999128, 106.68389797210693));
		        level3.add(new LatLng(10.751691495001378, 106.68200969696045));
		        level3.add(new LatLng(10.750047174054735, 106.6796064376831));
		        level3.add(new LatLng(10.749203929068173, 106.676344871521));
		        level3.add(new LatLng(10.749203929068173, 106.67325496673584));
		        level3.add(new LatLng(10.751016902876426, 106.66419982910156));
		        level3.add(new LatLng(10.75527524037449, 106.66295528411865));
		        level3.add(new LatLng(10.759069747889738, 106.66377067565918));
		        level3.add(new LatLng(10.761304268865539, 106.66561603546143));
		        
		        levels = new ArrayList<ArrayList<LatLng>>();
		        levels.add(level1);
		        levels.add(level2);
		        levels.add(level3);
		        
		        gonOpt1 = new PolygonOptions();
		        gonOpt2 = new PolygonOptions();
		        gonOpt3 = new PolygonOptions();
		        
		        gonOpt1.addAll(level1);
		        gonOpt1.fillColor(Color.BLUE);
		        gonOpt1.strokeColor(Color.BLUE);
		        gonOpt1.zIndex(9);
		        
		        gonOpt2.addAll(level2);
		        gonOpt2.fillColor(Color.RED);
		        gonOpt2.strokeColor(Color.RED);
		        gonOpt2.zIndex(6);
		        
		        gonOpt3.addAll(level3);
		        gonOpt3.fillColor(Color.GREEN);
		        gonOpt3.strokeColor(Color.GREEN);
		        gonOpt3.zIndex(3);
		        
		        gonOpts = new ArrayList<PolygonOptions>();
		        gonOpts.add(gonOpt1);
		        gonOpts.add(gonOpt2);
		        gonOpts.add(gonOpt3);		        
		        DrawPolyline(gonOpts);			       
			}
		});
        
	    }
    
//	private void DrawPolyline(ArrayList<List<LatLng>> pList){
//	for(int i = 0; i < pList.size(); i++ ){
////		gonOpt = new PolygonOptions();
//		if(i == 1){
//			
//		}
////		polygon = this.myMap.addPolygon(gonOpt);
//	}
//}
	  
    private void DrawPolyline(ArrayList<PolygonOptions> goptList){
		for(int i = 0; i < goptList.size(); i++ ){
			polygon = this.myMap.addPolygon(goptList.get(i));
		}
	}
	  
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		super.onCreateOptionsMenu(menu);
		menu.add("Go back home screen")
		.setIntent(new Intent(this, MainActivity.class));
		menu.add("Help")
		.setIntent(new Intent(this, Guide.class));
		menu.add("Sampling power")
		.setIntent(new Intent(this, SamplingPowPic.class));
		menu.add("Sampling pos and bandwidth")
		.setIntent(new Intent(this, SeekAp.class));
		menu.add("View pow map")
		.setIntent(new Intent(this, Displaypow.class));
		menu.add("View band map")
		.setIntent(new Intent(this, Displayband.class));
		menu.add("View wlan map")
		.setIntent(new Intent(this, Wlanmap.class));
		return true;
	}

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())
    	{
       case R.id.mainscreen:
        //Xử lý tro ve man hinh chinh
    	   break;
        case R.id.guide:
        //xử lý xem thông huong dan
        	break;
        case R.id.samplingpow:
        	break;
        case R.id.samplingband:
        	break;
        case R.id.powermap:
        	break;	
        case R.id.bandwidthmap:
        	break;	
        case R.id.wlanmap:
        	break;	
        }
        return super.onOptionsItemSelected(item);
        }

}
